#ifndef _TIMER_LIST_H_
#define _TIMER_LIST_H_

#include "Timer_Queue.h"

// Forward declaration.
template <class TYPE>
class Timer_List;


template <class TYPE>
class Timer_List_Iterator
	: public Timer_Queue_Iterator <TYPE>
{
public:
	typedef Timer_List<TYPE> List;
	/// Constructor.
	Timer_List_Iterator (List& lst);

	/// Destructor.
	virtual ~Timer_List_Iterator (void);

	/// Positions the iterator at the earliest node in the Timer Queue
	virtual void first (void);

	/// Positions the iterator at the next node in the Timer Queue
	virtual void next (void);

	/// Returns true when there are no more nodes in the sequence
	virtual int isdone (void) const;

	/// Returns the node at the current position in the sequence
	virtual Timer_Node<TYPE> *item (void);

protected:
	/// Pointer to the ACE_Timer_List that we are iterating over.
	List& list_;

	/// Current position in the ACE_Timer_List
	Timer_Node<TYPE>* current_node_;
};


///////////////////////////////////////////////////////////////////////////////

template <class TYPE>
class Timer_List : public Timer_Queue<TYPE>
{
public:
	/// Type of iterator
	typedef Timer_List_Iterator<TYPE> Iterator;

	/// Iterator is a friend
	friend class Timer_List_Iterator<TYPE>;

	typedef Timer_Node<TYPE> Node;
	/// Type inherited from
	typedef Timer_Queue<TYPE> Base;
	typedef std::list<Node> FreeList;

	// = Initialization and termination methods.
	/**
	* Default constructor. @a upcall_functor is the instance of the
	* FUNCTOR to be used by the list. If @a upcall_functor is 0, a
	* default FUNCTOR will be created.  @a freelist is the freelist of
	* timer nodes.  If 0, then a default freelist will be created.
	*/
	Timer_List (FreeList* freelist = 0);

	/// Destructor
	virtual ~Timer_List (void);

	/// True if queue is empty, else false.
	virtual int is_empty (void) const;

	/// Returns the time of the earlier node in the Timer_List.
	/// Must be called on a non-empty queue.
	virtual unsigned long earliest_time (void) const;

	virtual int reset_interval (long timer_id,
		unsigned long interval);

	virtual int cancel (const TYPE& type,
		int dont_call_handle_close = 1);

	virtual int cancel (long timer_id,
		const void** arg = 0,
		int dont_call_handle_close = 1);

	/// Returns a pointer to this Timer_Queue's iterator.
	virtual Timer_Queue_Iterator<TYPE>& iter (void);

	/// Removes the earliest node from the queue and returns it
	virtual Timer_Node<TYPE>* remove_first (void);

	/// Reschedule an "interval" Timer_Node. 
	virtual void reschedule (Timer_Node<TYPE> *);

	/// Reads the earliest node from the queue and returns it.
	virtual Timer_Node<TYPE>* get_first (void);

private:

	virtual long schedule_i (const TYPE& type,
		const void* arg,
		unsigned long future_time,
		unsigned long interval);

	void schedule_i(Timer_Node<TYPE>* n, unsigned long exp);

	Timer_Node<TYPE>* find_node(long timer_id) const;

	void cancel_i (Timer_Node<TYPE>* n);

	void unlink (Timer_Node<TYPE>* n);

	Timer_Node<TYPE>* get_first_i(void) const;

private:

	/// Pointer to linked list of <Timer_Handles>.
	Timer_Node<TYPE>* head_;

	/// Iterator used to expire timers.
	Iterator* iterator_;

	/**
	* Keeps track of the timer id that uniquely identifies each timer.
	* This id can be used to cancel a timer via the <cancel(long)>
	* method.
	*/
	long id_counter_;

};

#include "Timer_List.cpp"
#endif//_TIMER_LIST_H_